﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using OrderMgr.OrderInfo;
using OrderMgr.CustomerInfo;
using System.Collections;
using TierApplication;

namespace OrderMgr.OrderInfo
{
    public partial class frmBorrowSendBack : TierService.frmTemplate
    {

        int iOrderNum = 0;
            int iProductCount = 0;
        decimal dSumMoney = 0;

        public frmBorrowSendBack()
        {
            InitializeComponent();
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            ckbOrderDate.Checked = true;
            txtOrderDateStart.DateTime = DateTime.Now.AddDays(-7);
            txtOrderDateEnd.DateTime = DateTime.Now.Date;
        }

        private void frmSaleShip_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";
            string sShipStatus = ((int)Utility.OrderStatus.S0Input).ToString();
            ls_where = " and a.OrderType ='" + (int)Utility.OrderType.BorrowSendBack + "'";

            if (gFuncType != Utility.FuncType.Select)
            {
                ls_where = ls_where + " and a.OrderStatus ='" + (string)txtOrderStatus.EditValue + "'";
            }
            else
                ls_where = ls_where + " and a.OrderStatus ='" + (int)Utility.OrderStatus.S2Audited + "'";

            if ((txtOrderID.Text != "") && (ckbOrderID.Checked))
                ls_where = ls_where + " and a.OrderID like '%" + txtOrderID.Text + "%'";

            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                ls_where = ls_where + " and a.DepotID = '" + (string)txtDepotID.EditValue + "'";

            if ((txtOrderUserID.Text.Trim() != "") && (ckbOrderUserID.Checked))
                ls_where = ls_where + " and a.OrderUserID = '" + (string)txtOrderUserID.EditValue + "'";

            if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            {
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            }

            //if (sShipStatus == "2")
            //{
            //    if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            //    {
            //        ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
            //        ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            //    }
            //}
            //else
            //{
            //    if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            //    {
            //        ls_where = ls_where + " and Convert(varchar(10),a.ShipDate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
            //        ls_where = ls_where + " and Convert(varchar(10),a.ShipDate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            //    }
            //}


            if ((txtCustID.Text.Trim() != "") && (ckbCustID.Checked))
                ls_where = ls_where + " and a.CustID = '" + txtCustID.Text + "'";


            T_Order mOrder = new T_Order();
            DataSet dsOrder = mOrder.GetOrderList(ls_where);
            gcOrder.DataSource = dsOrder.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSaleShipStockDetail fDetail = new frmSaleShipStockDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.BorrowSendBack;
            fDetail.ShowDialog();

            btnSearch_Click(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.GetModel(sID);
            if (mOrder.OrderStatus != "0")
            {
                MessageBox.Show("此订单已提交审核，不能删除");
                return;
            }

            frmSaleShipStockDetail fDetail = new frmSaleShipStockDetail();
            fDetail.OpMode = Utility.OperateMode.Update;
            fDetail._OrderType = Utility.OrderType.BorrowSendBack;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
            btnSearch_Click(null, null);
        
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            //frmSaleShipDetail fDetail = new frmSaleShipDetail();
            //fDetail.OpMode = TierService.Utility.OperateMode.Query;
            //fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            //fDetail._OrderType = Utility.OrderType.Sale;
            //fDetail._OrderID = sID;
            //fDetail.ShowDialog();
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sOrderID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.Audit(sOrderID, Utility.gUserID);

            btnSearch_Click(null, null);

            //frmBuyDetail fDetail = new frmBuyDetail();
            //fDetail.OpMode = TierService.Utility.OperateMode.Query;
            //fDetail._OrderType = Utility.OrderType.Buy;
            //fDetail._AuditStatus = Utility.AuditStatus.Input;
            //fDetail._OrderID = sID;
            //fDetail.ShowDialog();
        }

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            btnQuery_Click(null, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此订单记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.GetModel(sID);
            if (mOrder.OrderStatus != "0")
            {
                MessageBox.Show("此订单已提交审核，不能删除");
                return;
            }

            mOrder.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtCustID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmCustomer fDetail = new frmCustomer();
            fDetail.gFuncType = Utility.FuncType.Select;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCustID.Text = fDetail._CustID;
            }
        }

        private void ceAll_CheckedChanged(object sender, EventArgs e)
        {
            //for (int i = 0; i < gvOrder.DataRowCount; i++)
            //{
            //    DataRow dr = gvOrder.GetDataRow(i);
            //    dr["IsChecked"] = ceAll.Checked ? "1" : "0";
            //}


            //iProductCount = 0;
            //iOrderNum = 0;
            //dSumMoney = 0;
            //for (int i = 0; i < gvOrder.DataRowCount; i++)
            //{
            //    DataRow dr = gvOrder.GetDataRow(i);
            //    if (dr["IsChecked"] == "1")
            //    {
            //        iProductCount = iProductCount + 1;
            //        iOrderNum = iOrderNum + Convert.ToInt16(Math.Floor(double.Parse(dr["OrderActQuantity"].ToString())));
            //        dSumMoney = dSumMoney + decimal.Parse(dr["OrderActMoney"].ToString());
            //    }
            //}
            //lblProductCOunt.Text = "选择单据数量：" + iProductCount.ToString() + ",商品总数量：" + iOrderNum.ToString() + ",总金额：" + dSumMoney.ToString();

        }

        private void gvOrder_Click(object sender, EventArgs e)
        {
                  }

        private void gvOrder_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (gvOrder.RowCount < 1) return;
            //DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            //dr["IsChecked"] = (dr["IsChecked"] == "1") ? "0" : "1";

            //iProductCount = 0;
            //iOrderNum = 0;
            //dSumMoney = 0;
            //for (int i = 0; i < gvOrder.DataRowCount; i++)
            //{
            //    DataRow drOrder = gvOrder.GetDataRow(i);
            //    if (drOrder["IsChecked"] == "1")
            //    {
            //        iOrderNum = iOrderNum + 1;
            //        iProductCount = iProductCount + Convert.ToInt32(Math.Floor(decimal.Parse(drOrder["OrderActQuantity"].ToString())));
            //        dSumMoney = dSumMoney + decimal.Parse(drOrder["OrderActMoney"].ToString());
            //    }
            //}
            //lblProductCOunt.Text = "选择单据数量：" + iProductCount.ToString() + ",商品总数量：" + iProductCount.ToString() + ",总金额：" + dSumMoney.ToString();

        }

        private void rgOrderStatus_EditValueChanged(object sender, EventArgs e)
        {
           
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sShipCompany = dr["ShipCompanyName"].ToString();
            string sOrderID = dr["OrderID"].ToString();

            string sFileName = Application.StartupPath + "\\物流单\\" + sShipCompany + ".repx";
            if (!System.IO.File.Exists(sFileName))
            {
                MessageBox.Show(sShipCompany + "快递模板不存在,不能打印此快递公司单!");
                return;
            }

            TOrderFunc.PintOrderShipPreview(sOrderID, sShipCompany);
        }

        private void btnPrintAll_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sShipID = dr["ShipID"].ToString();
            if (sShipID == "")
            {
                XtraMessageBox.Show("选择订单未发货，不能打印");
                return;
            }

            string sShipCompany = dr["ShipCompanyName"].ToString();    
            string sFileName = Application.StartupPath + "\\物流单\\" + sShipCompany + ".repx";
            if (!System.IO.File.Exists(sFileName))
            {
                MessageBox.Show(sShipCompany + "快递模板不存在,不能打印此快递公司单!");
                return;
            }

            for (int i = 0; i < gvOrder.DataRowCount; i++)
            {
                string sOrderID = gvOrder.GetDataRow(i)["OrderID"].ToString();
                string sOrderShipID = gvOrder.GetDataRow(i)["ShipID"].ToString();                
                if (sOrderShipID == sShipID)
                {
                    TOrderFunc.PintOrderShip(sOrderID, sShipCompany);
                }
            }
            
        }

        private void btnOrderShip_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sOrderID = dr["OrderID"].ToString();

            if (sOrderID != "")
            {
                TdsOrder dsOrder = TOrderFunc.GetOrderPrintList(sOrderID);
                RptOrderShip xrptOrderBuy = new RptOrderShip();
                xrptOrderBuy.DataSource = dsOrder;
                xrptOrderBuy.ShowPreviewDialog();

                T_Order mOrder1 = new T_Order();
                mOrder1.UpdatePrintStatus(sOrderID);
            }
        }
    }

}